package com.harmony.pilot.data.controller;

import com.harmony.pilot.common.dto.CodeContext;
import com.harmony.pilot.common.dto.CompletionItem;
import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.data.service.CodeQLService;
import com.harmony.pilot.data.service.Neo4jService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据服务控制器
 */
@Slf4j
@RestController
@RequestMapping("/data")
@RequiredArgsConstructor
public class DataController {
    
    private final CodeQLService codeQLService;
    private final Neo4jService neo4jService;
    
    /**
     * CodeQL查询符号
     */
    @PostMapping("/codeql/symbols")
    public RestResult<List<CompletionItem>> querySymbols(@RequestBody CodeContext context) {
        log.info("Querying symbols with CodeQL for: {}", context.getFilePath());
        
        try {
            List<CompletionItem> items = codeQLService.querySymbols(context);
            return RestResult.success(items);
        } catch (Exception e) {
            log.error("Failed to query symbols", e);
            return RestResult.error("Failed to query symbols: " + e.getMessage());
        }
    }
    
    /**
     * CodeQL代码分析
     */
    @PostMapping("/codeql/analyze")
    public RestResult<Map<String, Object>> analyzeCode(@RequestBody CodeContext context) {
        log.info("Analyzing code with CodeQL");
        
        try {
            Map<String, Object> result = codeQLService.analyzeCode(context);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to analyze code", e);
            return RestResult.error("Failed to analyze code: " + e.getMessage());
        }
    }
    
    /**
     * Neo4j查询相关符号
     */
    @PostMapping("/neo4j/related")
    public RestResult<List<CompletionItem>> queryRelatedSymbols(@RequestBody CodeContext context) {
        log.info("Querying related symbols from Neo4j");
        
        try {
            List<CompletionItem> items = neo4jService.queryRelatedSymbols(context);
            return RestResult.success(items);
        } catch (Exception e) {
            log.error("Failed to query related symbols", e);
            return RestResult.error("Failed to query related symbols: " + e.getMessage());
        }
    }
    
    /**
     * Neo4j查询调用链
     */
    @PostMapping("/neo4j/call-chain")
    public RestResult<Map<String, Object>> queryCallChain(@RequestParam String functionName) {
        log.info("Querying call chain for: {}", functionName);
        
        try {
            Map<String, Object> result = neo4jService.queryCallChain(functionName);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to query call chain", e);
            return RestResult.error("Failed to query call chain: " + e.getMessage());
        }
    }
    
    /**
     * Neo4j查询依赖关系
     */
    @PostMapping("/neo4j/dependencies")
    public RestResult<Map<String, Object>> queryDependencies(@RequestParam String moduleName) {
        log.info("Querying dependencies for: {}", moduleName);
        
        try {
            Map<String, Object> result = neo4jService.queryDependencies(moduleName);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to query dependencies", e);
            return RestResult.error("Failed to query dependencies: " + e.getMessage());
        }
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public RestResult<String> health() {
        return RestResult.success("Data Service is running");
    }
}

